package main

import (
	"fmt"
	"sync"
	"time"
)

var wg5 sync.WaitGroup
var lock5 sync.RWMutex

// 读写锁是可重入的锁， 对于读来说不会锁住资源, 所以这是并发
func read5() {
	defer wg5.Done()
	lock5.Lock()
	fmt.Println("正在读数据...")
	lock5.Unlock()
}

// 写入资源的时候会加锁
func write5() {
	defer wg5.Done()
	lock5.Lock()
	fmt.Println("正在写入数据...")
	time.Sleep(time.Second * 2)
	fmt.Println("写入数据完毕...")
	lock5.Unlock()
}

func main() {
	wg5.Add(6)
	for i := 0; i < 5; i++ {
		go read5()
	}
	go write5()
	wg5.Wait()
}
